package com.jblog.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportUtil<T> {

	public static String export(String fileName, String[] title, List<?> list) throws ClassNotFoundException {
		
		// 1.创建一个 workbook
		HSSFWorkbook workBook = new HSSFWorkbook();

		// 2.创建一个 worksheet
		HSSFSheet sheet = null;
		
		int num = 1;
		for(int j = 0; j<list.size() ; j++){
			if(j % 50000 == 0){
				sheet = workBook.createSheet("sheetName"+j);
				num = 1;
				HSSFRow row1 = sheet.createRow(0);
				for(int i =0;i<title.length;i++){
					HSSFCell cell = row1.createCell(i);
					cell.setCellValue(title[i]);
				}
			}
			
			HSSFRow row = sheet.createRow(num);
			Object obj = list.get(j);
			Class<?> c = obj.getClass();
			Field[] fields = c.getDeclaredFields();
			for(int i=0;i<fields.length;i++){
				String fieldName = fields[i].getName();
				String methodName = "get"+StringUtils.left(fieldName, 1).toUpperCase()+StringUtils.right(fieldName, fieldName.length()-1);
				try {
					Method method = c.getMethod(methodName, null);
					Object val = method.invoke(obj, null);
					HSSFCell cell = row.createCell(i);
					cell.setCellValue(val.toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
			num++;
		}
		
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(fileName);
			workBook.write(fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("Done!");
		return null;
	}
	
	public static void main(String[] args) throws ClassNotFoundException {
		
		String fileName = "D:/out.xlsx";
		File file = new File(fileName);
		if(file.exists()){
			file.delete();
		}
		
		List<User> list = new ArrayList<User>();
		for(int i = 0; i <500000; i++){
			User user = new User("Tom"+i, i, 10000.0+i, new Date());
			list.add(user);
		}
		
		
		ExportUtil.export("D:/out.xlsx", new String[]{"姓名","年龄","金钱","生日"}, list);
	}
}

class User{
	private String name;
	private int age;
	private Double money;
	private Date birthday;
	
	public User(String name, int age, Double money, Date birthday) {
		this.name = name;
		this.age = age;
		this.money = money;
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}